package log;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;

import java.util.HashMap;
import java.util.Map;

/**
 * @date 2017/11/23
 */
public class LogUtil {

    /**
     * 保存logger name和logger的对应关系，避免重复创建logger
     */
    private static final Map<String, Logger> LOGGER_MAP = new HashMap<>();

    private static final Logger logger = LoggerFactory.getLogger(LogUtil.class);

    /**
     * 根据调用栈的信息，得到调用日志方法的类，并根据此类创建或从缓存中得到loggger
     *
     * @date 2017/11/24 0:30
     */
    private static Logger getLogger() {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        boolean bl = false;
        // 调用栈中的class name
        String elementName;
        for (StackTraceElement element : elements) {
            elementName = element.getClassName();
            // 取第一个不等于LogUtils的类
            if (bl && !elementName.equals(LogUtil.class.getName())) {
                // 从缓存中获取logger
                Logger logger = LOGGER_MAP.get(elementName);
                if (logger == null) {
                    logger = LoggerFactory.getLogger(elementName);
                    LOGGER_MAP.put(elementName, logger);
                }
                return logger;
            }
            if (element.getClassName().equals(LogUtil.class.getName())) {
                bl = true;
            }
        }



        logger.warn("Could not get logger by stack strace, will use the default.");
        return logger;
    }

    /**
     * 判断是否调试处于打开状态
     *
     * @date 2017/11/28 20:35
     */
    public static boolean isDebugEnabled() {
        return logger.isDebugEnabled();
    }

    /**
     * 判断是否info处于打开状态
     *
     * @date 2017/11/28 20:37
     */
    public static boolean isInfoEnabled() {
        return logger.isInfoEnabled();
    }

    /**
     * 判断Trace是否处于打开状态
     *
     * @date 2017/11/28 20:37
     */
    public static boolean isTraceEnabled() {
        return logger.isTraceEnabled();
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(String)
     */
    public static void trace(String msg) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trgace(String, Object)
     */
    public static void trace(String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(String, Object, Object)
     */
    public static void trace(String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            getLogger().trace(format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(String, Object...)
     */
    public static void trace(String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            getLogger().trace(format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(String, Throwable)
     */
    public static void trace(String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(Marker, String)
     */
    public static void trace(Marker marker, String msg) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(marker, msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(Marker, String, Object)
     */
    public static void trace(Marker marker, String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(marker, format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(Marker, String, Object, Object)
     */
    public static void trace(Marker marker, String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(marker, format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(Marker, String, Object...)
     */
    public static void trace(Marker marker, String format, Object... argArray) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(marker, format, argArray);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#trace(Marker, String, Throwable)
     */
    public static void trace(Marker marker, String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(marker, msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(String, Object)
     */
    public static void debug(String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(String)
     */
    public static void debug(String msg) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(String, Object, Object)
     */
    public static void debug(String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(String, Object...)
     */
    public static void debug(String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(String, Throwable)
     */
    public static void debug(String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(Marker, String)
     */
    public static void debug(Marker marker, String msg) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(marker, msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(Marker, String, Object)
     */
    public static void debug(Marker marker, String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(marker, format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(Marker, String, Object, Object)
     */
    public static void debug(Marker marker, String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(marker, format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(Marker, String, Object...)
     */
    public static void debug(Marker marker, String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(marker, format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#debug(Marker, String, Throwable)
     */
    public static void debug(Marker marker, String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(marker, msg, t);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(String)
     */
    public static void info(String msg) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(String, Object)
     */
    public static void info(String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(String, Object, Object)
     */
    public static void info(String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(String, Object...)
     */
    public static void info(String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(String, Throwable)
     */
    public static void info(String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(Marker, String)
     */
    public static void info(Marker marker, String msg) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(marker, msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(Marker, String, Object)
     */
    public static void info(Marker marker, String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(marker, format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(Marker, String, Object, Object)
     */
    public static void info(Marker marker, String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(marker, format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(Marker, String, Object...)
     */
    public static void info(Marker marker, String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(marker, format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#info(Marker, String, Throwable)
     */
    public static void info(Marker marker, String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(marker, msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(String)
     */
    public static void warn(String msg) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(msg);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(String, Object)
     */

    public static void warn(String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(String, Object...)
     */
    public static void warn(String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(String, Object, Object)
     */
    public static void warn(String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(String, Throwable)
     */
    public static void warn(String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(Marker, String)
     */
    public static void warn(Marker marker, String msg) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(marker, msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(Marker, String, Object)
     */
    public static void warn(Marker marker, String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(marker, format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(Marker, String, Object, Object)
     */
    public static void warn(Marker marker, String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(marker, format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(Marker, String, Object...)
     */
    public static void warn(Marker marker, String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(marker, format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#warn(Marker, String, Throwable)
     */
    public static void warn(Marker marker, String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(marker, msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(String)
     */
    public static void error(String msg) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(String, Object)
     */
    public static void error(String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(String, Object, Object)
     */
    public static void error(String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(String, Object...)
     */
    public static void error(String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(String, Throwable)
     */
    public static void error(String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(msg, t);
        }
    }


    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(Marker, String)
     */
    public static void error(Marker marker, String msg) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(marker, msg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(Marker, String, Object)
     */
    public static void error(Marker marker, String format, Object arg) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(marker, format, arg);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(Marker, String, Object, Object)
     */
    public static void error(Marker marker, String format, Object arg1, Object arg2) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(marker, format, arg1, arg2);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(Marker, String, Object...)
     */
    public static void error(Marker marker, String format, Object... arguments) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(marker, format, arguments);
        }
    }

    /**
     * @date 2017/11/24 16:20
     * @see org.slf4j.Logger#error(Marker, String, Throwable)
     */
    public static void error(Marker marker, String msg, Throwable t) {
        Logger logger = getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(marker, msg, t);
        }
    }
}

